package com.yang.mapper;

import com.yang.pojo.Defect;
import org.apache.commons.beanutils.BeanUtils;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchScrollRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
public class meta {

    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;

    public List<Defect> testMatchAll() throws IOException {
        SearchRequest request = new SearchRequest("defect001");

        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();


        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        sourceBuilder.size(1000);
        request.source(sourceBuilder.query(QueryBuilders.matchAllQuery()));
        request.scroll(TimeValue.timeValueMinutes(1L));//设置滚动时间间隔



        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        String scrollId = response.getScrollId();

        List<Defect> result = processSearchResponse(response); // 处理搜索响应数据


//        System.out.println("记录数有：" + hits.getTotalHits());
        System.out.println("响应时间是：" + response.getTook());


        // 使用滚动ID循环获取全部数据
        while (scrollId != null) {
            SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
            scrollRequest.scroll(TimeValue.timeValueMinutes(1L)); // 设置滚动时间间隔
            response = client.scroll(scrollRequest, RequestOptions.DEFAULT);
            scrollId = response.getScrollId();
//            处理搜索响应结果
            result.addAll(processSearchResponse(response));

        }

        return result;
    }

    private List<Defect> processSearchResponse(SearchResponse response) {
        // 处理搜索响应数据，将结果转换为实体类列表
        List<Map<String,Object>> maps = new ArrayList<>();

        for (SearchHit hit : response.getHits().getHits()) {
//            System.out.println(hit.getSourceAsMap());
            maps.add(hit.getSourceAsMap());
        }
        // 这里可以根据你的需求进行数据处理，例如使用ObjectMapper将JSON数据转换为实体类对象
        List<Defect> list = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            Defect defect = new Defect();
            try {
                BeanUtils.populate(defect,map);

            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            }
            list.add(defect);
        }
        // 返回处理后的结果列表
        return list;
    }
}
